<template>
  <div class="el-card cardBorder">
    <div class="operate">
      <el-button type="primary" @click="addCourse">添加</el-button>
      <el-input style="width: 30%;margin: 0 5px" v-model="searchParams.courseName" placeholder="请输入课程名称"></el-input>
      <el-select v-model="searchParams.userId" placeholder="请选择教师" style="margin: 0 5px">
        <el-option v-for="(item, index) in teachers" :key="index" :label="item.nickName"
                   :value="item.userId"></el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="getAllCourse">搜索</el-button>
    </div>
    <el-table
        :data="courses"
        border
        style="width: 100%;height: 100%;margin-bottom: 10px">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="courseName" label="课程名"></el-table-column>
      <el-table-column prop="userId" label="教师id"></el-table-column>
      <el-table-column  label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.userType!=='0'">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"  v-if="scope.row.userType!=='0'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="searchParams.pageNum"
        :page-size="searchParams.pageSize"
        :page-sizes="[5, 8, 10, 20]"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
    <course-dialog :visible.sync="addDialog" title="添加课程" :teachers="teachers" @addResult="getAllCourse"></course-dialog>
    <course-update-dialog :visible.sync="updateDialog" :course="updateCourse" :teachers="teachers" @addResult="getAllCourse"></course-update-dialog>
  </div>
</template>

<script>


import {deleteCourse, getCourseByPage} from "@/api/course";
import {getAllUser} from "@/api/user";
import CourseDialog from "@/components/dialog/courseDialog";
import CourseUpdateDialog from "@/components/dialog/courseUpdateDialog";

export default {
  name: "index",
  components: {CourseUpdateDialog, CourseDialog},
  data(){
    return{
      searchParams:{
        pageNum: 1,
        pageSize: 5,
        courseName:'',
        userId:''
      },
      courses:[],
      teachers:[],
      total:10,
      addDialog:false,
      updateCourse:{},
      updateDialog:false
    }
  },
  methods:{
    getAllCourse(){
      getCourseByPage(this.searchParams).then(res=>{
        if (res.code===200){
          this.$message.success("课程数据获取成功")
          this.courses=res.data.records;
          this.total=res.data.total;
        }
      })
    },
    handleSizeChange(val){
      this.getAllCourse();
    },
    handleCurrentChange(val){
      this.getAllCourse();
    },
    handleEdit(index,row){
      this.updateCourse=JSON.parse(JSON.stringify(row));
      this.updateDialog=true;
    },
    handleDelete(index,row){
      deleteCourse(row.id).then(res=>{
        if (res.code===200){
          this.$message.success("成功删除")
          this.getAllCourse();
        }
      })
    },
    addCourse(){
      this.addDialog=true;
    }
  },
  mounted() {
    this.getAllCourse();
    getAllUser().then(res=>{
      if (res.data!=null){
        for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].userType==='1'){
              this.teachers.push({'userId':res.data[i].id,'nickName':res.data[i].nickName})
            }
        }
      }
      console.log(this.teachers)
    })
  }
}
</script>

<style lang="less" scoped>
.cardBorder{
  width: 96%;
  padding: 20px;
  .operate{
    margin: 16px;
  }
}
</style>
